package net.niufei.study.cpatterns.activeobject;

public class ActivationQueue {
	
	private final Request[] requests;
	private int head;
	private int tail;
	private int count;

	public ActivationQueue(int capacity) {
		requests = new Request[capacity];
	}
	
	public synchronized void putRequest(Request request)
			throws InterruptedException {
		while (count == requests.length) {
			wait();
		}

		requests[tail++] = request;
		if (tail == requests.length)
			tail = 0;
		count++;
		
		notifyAll();
	}

	public synchronized Request takeRequest() throws InterruptedException {
		while (count == 0) {
			wait();
		}
		
		Request request = requests[head++];
		if (head == requests.length)
			head = 0;		
		count--;
		
		notifyAll();
		return request;
	}
}
